package cn.com.demo.controller;

import cn.com.demo.pojo.User;
import cn.com.demo.service.UserService;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;


@Controller
@RequestMapping("/demo")
public class DemoController {
    @Resource
    private UserService userService;

    @RequestMapping("/index")
    public String index(){
        return "index";
    }
    @RequestMapping("/hello")
    public String demo(String name, String sex, Model model){
        String result = "demo";
        model.addAttribute("name",name);
        model.addAttribute("sex", sex);
        return result;
    }
    @RequestMapping("/userList")
    public String userList(Model model){
        List<User> list= userService.getUser();
        model.addAttribute("userList",list);
        return "userList";
    }
    @RequestMapping(value = "/zhuce",method = RequestMethod.GET)
    public String userZist(@ModelAttribute User user){
    return "zhuce";
    }
    @RequestMapping(value = "/zhuce",method = RequestMethod.POST)
    public String userZist2(User user,Model model){
        System.out.println(user);
        int i = userService.addUser(user);
        if (i>0){
            List<User> user1 = userService.getUser();
            model.addAttribute("user1",user1);
            return "demo2";
        }else{
            return "zhuce";
        }
    }
}